package groupby.action;

import groupby.manage.GroupCriticismManage;
import groupby.manage.GroupbyManage;
import groupby.model.Category;
import groupby.model.Groupby;
import groupby.util.Pages;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import system.manage.SystemManage;
import system.model.SystemInfor;
import user.manage.UsersManage;
import user.model.Dynamic;
import user.model.UserDetial;
import user.model.Users;

import com.opensymphony.xwork2.ActionSupport;

import dic.manage.DicManage;
import dic.model.City;

public class GroupbyAction extends ActionSupport {

	private String title=null;
	
	private GroupbyManage groupbyManage=null;
	private SystemManage systemManage=null;
	private SystemInfor systemInfor=null;
	private GroupCriticismManage  groupCriticismManage=null;
	private UsersManage userManage = null;
	private DicManage dicManage=null;
	private int currentPage;
	private int rows;
	private String startDate=null;
	private String endDate=null;
    private JSONObject result;
    private String id=null;
    private String categoryId=null;
    private String categoryName=null;
    private List<Category>  categoryListAll=null;
	private List<Groupby> groupbyTodayListAll=null;
	private List<Groupby> groupbyBefore=null;
	private String path=null;
	private List listDredgeCitys=null;//获得开放城市列表
	private List oldGroupbyList=null;
	private List oldGroupbyMoreList=null;
	private String keyword=null;
	private Pages pages=null;
	private String paramStr=null;
	private String falg=null;
	private String sortFlag=null;
	private String searchKeyword=null;
	private String sort=null;
	private String tags=null;
	private List groupbyCriticismList=null;
	private int cityId;
	private String date=null;
	private String flag=null;
	private int page;
	private int userId;
	public String execute() {
		System.out.println("yes");
    	return "success";
    }
	 //获得所有groupby数据列表
    public String index()
    {
    	
         categoryListAll=(ArrayList<Category>)groupbyManage.getCategoryAll(1);
       
    	 Pages pages=new Pages();
	      pages.setCurrentPage(currentPage);
	      pages.setPageSize(8);
	      groupbyTodayListAll=groupbyManage.getGroupbyTodayListAll(pages,cityId+"",categoryId,keyword,"");
	      groupbyBefore=groupbyManage.getBeforeList(cityId+"");
	      
	      listDredgeCitys=dicManage.getDredgeCitys();
	      Pages pageTemp=new Pages();
	      pageTemp.setCurrentPage(1);
	      pageTemp.setPageSize(6);
	      groupbyCriticismList=groupCriticismManage.getNewGroupbyCriticism(pageTemp);
	      String path = ServletActionContext.getRequest().getRealPath(
			"/WEB-INF/classes/tags.xml");
	      tags=groupbyManage.getTags(path);
	      
	      this.pages=pages;
    	 return "success";
    }
    //获得所在成市的数据列表
    @SuppressWarnings("unchecked")
	public String getCityGroupbyList()
    {
         
         categoryListAll=(ArrayList<Category>)groupbyManage.getCategoryAll(1);
       
    	 Pages pages=new Pages();
	      pages.setCurrentPage(currentPage);
	      pages.setPageSize(8);
	      
	      if(keyword!=null)
	      {
	    	  try {
				keyword=new String(keyword.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			} 
	      }
	      String[] keywordTemp=null;
	    	  if(keyword!=null)
	    	  {
	              keywordTemp=keyword.split(",");
	              groupbyTodayListAll=groupbyManage.getGroupbyTodayListAll(pages,cityId+"",categoryId,keywordTemp[0],sortFlag);
	    	  }else
	    	  {
	    		 
	    		  groupbyTodayListAll=groupbyManage.getGroupbyTodayListAll(pages,cityId+"",categoryId,"",sortFlag);
	    	  }
	     
	      groupbyBefore=groupbyManage.getBeforeList(cityId+"");
	      listDredgeCitys=dicManage.getDredgeCitys();
	      
	      
	      
	      
	      paramStr="groupby!getCityGroupbyList.action?cityId="+cityId+"";
	      searchKeyword=paramStr;
	      if(categoryId!=null)
	      {
	    	  paramStr+="&categoryId="+categoryId;
	    	  searchKeyword+="&categoryId="+categoryId;
	      }
	      String href="";
	      if(keyword!=null)
	      {
	    		  paramStr+="&keyword="+keyword;
	    		  href="&keyword="+keyword;
	      }
	      
	      City city=dicManage.findCityById(Integer.parseInt(cityId+""));
	      path="<a style='color:#9C2684;font-family:微软雅黑;font-size:14px;font-weight:bold;' href='groupby!index.action?currentPage=1&cityId="+cityId+"'>"+city.getName()+"</a>";
	      
	      
	      if(categoryId!=null)
	      {
	    	  Category category=groupbyManage.findCategoryById(Integer.parseInt(categoryId));
		      path+=" > <a style='color:#9C2684;font-family:微软雅黑;font-size:14px;font-weight:bold;' href='groupby!getCityGroupbyList.action?cityId="+cityId+"&categoryId="+categoryId+href+"&currentPage="+currentPage+"'>"+category.getCategoryName()+"</a>";
	      }
	      
	      
	      if(sortFlag!=null)
	      {
	    	  paramStr+="&sortFlag="+sortFlag;
	    	  searchKeyword+="&sortFlag="+sortFlag;
	      }  
	      paramStr+="&currentPage=";
	      searchKeyword+="&currentPage="+currentPage;
	      String path = ServletActionContext.getRequest().getRealPath(
			"/WEB-INF/classes/tags.xml");
	      if(keyword!=null)
	      {
		      if(keywordTemp.length==1)
		      {
		         tags=groupbyManage.getTags(path);
		      }else
		      {
		    	  tags = userManage.getSingleUser(userId).getTags();
		      }
	      }else
	      {
	    	  tags=groupbyManage.getTags(path);
	      }
	      this.pages=pages;
	      Pages pageTemp=new Pages();
	      pageTemp.setCurrentPage(1);
	      pageTemp.setPageSize(6);
	      groupbyCriticismList=groupCriticismManage.getNewGroupbyCriticism(pageTemp);
    	 return "CityGroupbyList";
    }
   //获得groupby列表
    public String getGroupbyList() {
    	Pages pages=new Pages();
    	      pages.setCurrentPage(page);
    	      pages.setPageSize(rows);
    	      result=groupbyManage.getGroupbyList(title,startDate,endDate,categoryId, pages,cityId);
    	      
        return "success";
    }
  //更新groupby列表
    public String updateGroupby() {
    	      result=groupbyManage.updateGroupby( id, categoryId);
    	      System.out.println(result.toString());
        return "success";
    }
    
    
    //删除grouby
    public String delete() {
    	
    	      result=groupbyManage.delete(id);
    	     
        return "success";
    }
  //获得grouby类别
    public String getCategoryList() {
    	Pages pages=new Pages();
	      pages.setCurrentPage(page);
	      pages.setPageSize(rows);
    	      result=groupbyManage.getCategory(pages,flag);
        return "success";
    }
  //添加grouby类别
    public String addCategory() {
    	Category category=new Category();
    	try {
			categoryName = new String(categoryName.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	category.setCategoryName(categoryName);	
    	category.setSort(Integer.parseInt(flag));
    	      result=groupbyManage.addCategory(category);   	    
        return "success";
    }
  //修改grouby类别
    public String editCategory() {
    	Category category=new Category();
    	category.setId(Integer.parseInt(id));
    	try {
			categoryName = new String(categoryName.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	category.setCategoryName(categoryName);
    	category.setSort(Integer.parseInt(flag));
    	      result=groupbyManage.editCategory(category);
        return "success";
    }
    //删除category
    public String deleteCategory()
    {
    	result=groupbyManage.deleteCategory(id);
        return "success";
    }
    
    //获得category select list
    public String getSelectList()
    {
  	      result=groupbyManage.getSelectList(flag);
  	   
      return "success";
    }
    
    //获得top数据
    public String top()
    {
    	Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
    	if(users!=null)
    	{
    		String[] tempId=id.split(",");
	    	result=groupbyManage.getTop(tempId[0]);
	    	Dynamic  dynamic=new Dynamic();
	    	
			UserDetial userDetial=userManage.getSingleUser(users.getId());
			Groupby groupby = groupbyManage.criticismGroupbyId(Integer.parseInt(tempId[0]));
			if(userDetial!=null&&!userDetial.getUsername().equals(""))
			{
			   dynamic.setUser1(userDetial.getUsername()+"<>"+users.getId());
			}else
			{
				dynamic.setUser1(users.getEmail()+"<>"+users.getId());
			}
			dynamic.setUser2(groupby.getWebsite()+"<>"+tempId[0]+","+currentPage+","+cityId);
			dynamic.setContent("");
			dynamic.setFlag(Integer.parseInt(tempId[1]));
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dynamic.setDatetime(sdf.format(new Date()));
			userManage.addDynamic(dynamic);
    	
    	}else
    	{
    		result=new JSONObject();
    		result.put("success","nologin");
    		
    	}
    	return "success";
    }
 // 获得往期团购
	public String oldGroupbyList() {
		pages = new Pages();
		pages.setCurrentPage(currentPage);
		pages.setPageSize(2);
		 if(keyword!=null)
	      {
	    	  try {
				keyword=new String(keyword.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			} 
	      }
		oldGroupbyList =groupbyManage.oldGroupbyList("",keyword,sort,pages,cityId);
		String path = ServletActionContext.getRequest().getRealPath(
		"/WEB-INF/classes/tags.xml");
		tags=groupbyManage.getTags(path);
		listDredgeCitys=dicManage.getDredgeCitys();
		 Pages pageTemp=new Pages();
	      pageTemp.setCurrentPage(1);
	      pageTemp.setPageSize(6);
	      groupbyCriticismList=groupCriticismManage.getNewGroupbyCriticism(pageTemp);
	      groupbyBefore=groupbyManage.getBeforeList(cityId+"");
	     
		return "oldGroupbyList";
	}
	// 获得往期更多团购
	public String oldGroupbyMoreList() {
		pages = new Pages();
		pages.setCurrentPage(currentPage);
		pages.setPageSize(8);
		 if(keyword!=null)
	      {
	    	  try {
				keyword=new String(keyword.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			} 
	      }
		 String startTime=date+" 00:00:00";
		 String endTime=date+" 23:59:59";
		 Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
		 String username="";
		 if(users!=null&&falg!=null&&!falg.equals(""))
		 {
			 username=userManage.getSingleUser(users.getId()).getUsername();
		 }
		 oldGroupbyMoreList =groupbyManage.oldGroupbyListByPage(categoryId,  keyword,startTime,endTime, sort,pages,cityId,username);
		 String path = ServletActionContext.getRequest().getRealPath(
			"/WEB-INF/classes/tags.xml");
		 tags=groupbyManage.getTags(path);
		listDredgeCitys=dicManage.getDredgeCitys();
		 Pages pageTemp=new Pages();
	      pageTemp.setCurrentPage(1);
	      pageTemp.setPageSize(6);
	      groupbyCriticismList=groupCriticismManage.getNewGroupbyCriticism(pageTemp);
	      groupbyBefore=groupbyManage.getBeforeList(cityId+"");
	      categoryListAll=(ArrayList<Category>)groupbyManage.getCategoryAll(1);
	      paramStr="groupby!oldGroupbyMoreList.action?cityId="+cityId+"&date="+date;
	      if(keyword!=null&&!keyword.equals(""))
	      {
	    	  paramStr+="&keyword="+keyword;
	      }
	      if(sort!=null&&!sort.equals(""))
	      {
	    	  paramStr+="&sort="+sort;
	      }
	      paramStr+="&currentPage=";
	      
		return "oldGroupbyMoreList";
	}
    public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
    public void setTitle(String title) {
        this.title = title;
    }

   
    public JSONObject getResult() {
        return result;
    }
    
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getTitle() {
		return title;
	}

	

	public GroupbyManage getGroupbyManage() {
		return groupbyManage;
	}

	public void setGroupbyManage(GroupbyManage groupbyManage) {
		this.groupbyManage = groupbyManage;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getCategoryName() {
		return categoryName;
	}
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	public String getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
	public List getCategoryListAll() {
		return categoryListAll;
	}
	public void setCategoryListAll(List categoryListAll) {
		this.categoryListAll = categoryListAll;
	}
	public List<Groupby> getGroupbyTodayListAll() {
		return groupbyTodayListAll;
	}
	public Pages getPages() {
		return pages;
	}
	public List<Groupby> getGroupbyBefore() {
		return groupbyBefore;
	}
	public List getListDredgeCitys() {
		return listDredgeCitys;
	}
	public DicManage getDicManage() {
		return dicManage;
	}
	public void setDicManage(DicManage dicManage) {
		this.dicManage = dicManage;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getPath() {
		return path;
	}
	public String getParamStr() {
		return paramStr;
	}
	public String getSort() {
		return sort;
	}
	public String getSortFlag() {
		return sortFlag;
	}
	public void setSortFlag(String sortFlag) {
		this.sortFlag = sortFlag;
	}
	public String getSearchKeyword() {
		return searchKeyword;
	}
	public String getTags() {
		return tags;
	}
	public GroupCriticismManage getGroupCriticismManage() {
		return groupCriticismManage;
	}
	public void setGroupCriticismManage(GroupCriticismManage groupCriticismManage) {
		this.groupCriticismManage = groupCriticismManage;
	}
	public List getGroupbyCriticismList() {
		return groupbyCriticismList;
	}
	public UsersManage getUserManage() {
		return userManage;
	}
	public void setUserManage(UsersManage userManage) {
		this.userManage = userManage;
	}
	public int getCityId() {
		return cityId;
	}
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}
	public List getOldGroupbyList() {
		return oldGroupbyList;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public List getOldGroupbyMoreList() {
		return oldGroupbyMoreList;
	}
	public String getDate() {
		return date;
	}
	public String getFalg() {
		return falg;
	}
	public void setFalg(String falg) {
		this.falg = falg;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public SystemInfor getSystemInfor() {
		return systemInfor;
	}
	public SystemManage getSystemManage() {
		return systemManage;
	}
	public void setSystemManage(SystemManage systemManage) {
		this.systemManage = systemManage;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	

}
